package com.example.demomk.Adapter;




import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demomk.DetailsActivity;
import com.example.demomk.R;
import com.example.demomk.databinding.ItemUserBinding;
import com.example.demomk.entity.AvgMeasurementWithDetails;

import java.util.ArrayList;
import java.util.List;

public class MeasurementAdapter extends RecyclerView.Adapter<MeasurementAdapter.ViewHolder>{
    private List<AvgMeasurementWithDetails> avglist = new ArrayList<>();
    private List<Boolean> selectedItems = new ArrayList<>();

    //定义监听复选框接口
    public interface OnItemSelectedListener {
        void onItemSelected(int position, boolean isChecked);
    }

    private OnItemSelectedListener listener;

    //定义切换详情页面接口
    public interface OnItemClickListener {
        void onItemClick(int position,int id);
    }
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.listener = listener;
    }
    public void setMeasurements(List<AvgMeasurementWithDetails> measurements) {
        if (measurements == null) {
            this.avglist.clear();
            this.selectedItems.clear();
        } else {
            this.avglist = measurements;
            this.selectedItems.clear();
            for (int i = 0; i < measurements.size(); i++) {
                this.selectedItems.add(false);
            }
        }
        notifyDataSetChanged();
    }
    public void selectAll(boolean isSelected) {
        for (int i = 0; i < selectedItems.size(); i++) {
            selectedItems.set(i, isSelected);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MeasurementAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemUserBinding binding = ItemUserBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MeasurementAdapter.ViewHolder holder, int position) {
       AvgMeasurementWithDetails item = avglist.get(position);
       Boolean isSelected = selectedItems.get(position);

       // 关键修复：每次绑定都更新 ImageView 状态
        holder.userSelect.setImageResource(isSelected ?
                R.drawable.check_box_checked :
                R.drawable.check_box_unchecked);

        // 点击切换选中状态
        holder.userSelect.setOnClickListener(v -> {
            boolean isChecked = !selectedItems.get(position);
            selectedItems.set(position, isChecked);
            holder.userSelect.setImageResource(isChecked ?
                    R.drawable.check_box_checked :
                    R.drawable.check_box_unchecked);
            if (listener != null) listener.onItemSelected(position, isChecked);
        });

        holder.tvId.setText(String.valueOf(item.getId()));
        holder.tvStartTime.setText(item.start_time);
        holder.tvGroupName.setText(item.group_name); // Assuming group is in Location
        holder.tvLocationName.setText(item.location_name);
        holder.tvUserName.setText(item.user_name);
        if(item.is_active == 1){
            holder.tvStatus.setImageResource(R.drawable.green_dot);
        }else{
            holder.tvStatus.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(v.getContext(), DetailsActivity.class);
//                intent.putExtra("measurementId", item.id);
//                v.getContext().startActivity(intent);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

                int actualPosition = holder.getAdapterPosition();
                if (onItemClickListener != null){
                    onItemClickListener.onItemClick(actualPosition,item.id);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return avglist.size();
    }
    //获取选中状态
    public List<Boolean> getSelectedItems() {
        return selectedItems;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvId, tvStartTime, tvGroupName, tvLocationName, tvUserName;
        ImageView tvStatus ,  userSelect;
        public ViewHolder(@NonNull ItemUserBinding  binding ) {
            super(binding.getRoot());
            tvId = binding.tvId;
            tvStartTime = binding.tvStartTime;
            tvGroupName = binding.tvGroupName;
            tvLocationName = binding.tvLocationName;
            tvUserName = binding.tvUserName;
            tvStatus = binding.userStatus;
            userSelect = binding.userSelect;
        }
    }

    // 获取选中的ID
    public List<AvgMeasurementWithDetails> getSelectedMeasurements() {
        List<AvgMeasurementWithDetails> selectedMeasurements = new ArrayList<>();
        for (int i = 0; i < avglist.size(); i++) {
            if (i < selectedItems.size() && selectedItems.get(i)) {
                selectedMeasurements.add(avglist.get(i));
            }
        }
        return selectedMeasurements;
    }
}
